/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Eventos;

import InterfaceEI.JanelaPrincipal;
import InterfaceEI.PanelPrincipal;
import Utils.Utilidades;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Isabela
 */
public class MouseEventosMomento implements MouseListener, MouseMotionListener{
    PanelPrincipal panelP;
    boolean desenhou = false;
    double forca = 0;

    JanelaPrincipal frame;

    Utilidades utili = new Utilidades();

    public MouseEventosMomento(PanelPrincipal panelP, JanelaPrincipal frame) {
        this.panelP = panelP;
        this.frame = frame;
    }



    public void mouseClicked(MouseEvent e) {
        panelP.setMomentoFletor(false);
        if(!desenhou){

//            panelP.removeMouseListener(frame.getMouseDistribuida());
//            panelP.removeMouseMotionListener(frame.getMouseDistribuida());

           

            if((e.getX() >= panelP.getxInicio()) && (e.getX() <= panelP.getxFim())){
                panelP.setxCMomento(e.getX());
                desenhou = true;
                panelP.setDesenhouCM(true);
                String t = new String();
                if(frame.getForcaM() != 0){
                   // t = JOptionPane.showInputDialog(null, "Entre com a intensidade da carga (N):", "Carga Concentrada", JOptionPane.QUESTION_MESSAGE);
                }
                else{
                    boolean auxB = false;
                    do {
                        do {
                            t = JOptionPane.showInputDialog(null, "Entre com a intensidade da carga (N):", "Carga Concentrada", JOptionPane.QUESTION_MESSAGE);
                        } while (t == null || t.equalsIgnoreCase(""));

                        if (!t.equalsIgnoreCase("''")) {
                            try {
                                forca = Double.parseDouble(t);
                                frame.setForcaM(forca);
                                auxB = false;
                            } catch (NumberFormatException ex) {
                                auxB = true;
                            }
                        }
                    } while (auxB);
                }



                double x = utili.conversaoPixelMX((e.getX() - panelP.getxInicio()), panelP.getWidth());
                frame.setxMomento(x);

                frame.setTextForcaMom();
                frame.setTextXMom();

                if (frame.getEnableCarga()) {
                    frame.calcula();
                    frame.converteGrafico();

                    if (frame.getExibeGraficoEC().isSelected()) {
                        panelP.setCortante(frame.getPontosCortante());

                        panelP.setGraficoCortante(true);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeGraficoMF().isSelected()) {
                        panelP.setFletor(frame.getPontosFletor());

                        panelP.setGraficoFletor(true);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeGraficoD().isSelected()) {
                        panelP.setDeformada(frame.getPontosDeformada());

                        panelP.setGraficoDeformada(true);

                        panelP.repaint();
                        panelP.validate();
                    }

                    if(panelP.getFixoD() || panelP.getDesliF()){
                        panelP.setReacaoBiapoiadaA(true);
                        panelP.setReacaoBiapoiadaB(true);

                        panelP.setBiapoiadaA(frame.getDiagramas().getReacaoBiemA());
                        panelP.setBiapoiadaB(frame.getDiagramas().getReacaoBiemB());
                    }
                    else{
                        panelP.setReacaoBiapoiadaA(false);
                        panelP.setReacaoBiapoiadaB(false);

                        panelP.setReacaoEngastada(true);
                        panelP.setValorReacaoE(frame.getDiagramas().getReacaoApoioEngastado());
                    }

                }

                panelP.repaint();
                panelP.validate();
            }
            else{
                JOptionPane.showMessageDialog(null, "A carga está fora da viga!");
                desenhou = false;
            }
//            if (e.getX() < panelP.getxInicio()){
//                panelP.setxConcentrada(panelP.getxInicio());
//            }
//            else if(e.getX() > panelP.getxFim()){
//                panelP.setxConcentrada(panelP.getxFim());
//            }
//            else{
//
//            }
            panelP.addMouseListener(frame.getMouseDistribuida());
            panelP.addMouseMotionListener(frame.getMouseDistribuida());

            panelP.addMouseListener(frame.getMouseConcentrada());
            panelP.addMouseMotionListener(frame.getMouseConcentrada());
        }
        else{
            //MouseEventosDistribuida m = frame.getMouseDistribuida();
            
            if((e.getX() >= panelP.getxCMomento() - 20) && (e.getX() <= panelP.getxCMomento() + 20)){
                if((e.getY() >= panelP.getyInicio() - 20) && (e.getY() <= panelP.getyInicio() + 35)){
                    desenhou = false;
                    panelP.setDesenhouCM(false);

                    panelP.removeMouseListener(frame.getMouseDistribuida());
                    panelP.removeMouseMotionListener(frame.getMouseDistribuida());

                    panelP.removeMouseListener(frame.getMouseConcentrada());
                    panelP.removeMouseMotionListener(frame.getMouseConcentrada());

                    panelP.repaint();
                    panelP.validate();
                    //System.out.println("Tamanho: " + panelP.getTamanhoConcentrada());
                }
            }
//            else{
//                panelP.addMouseListener(frame.getMouseDistribuida());
//                panelP.addMouseMotionListener(frame.getMouseDistribuida());
//            }
        }
        panelP.repaint();
        panelP.validate();

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
        if (!desenhou) {
            //if ((e.getX() >= panelP.getxInicio()) && (e.getX() <= panelP.getxFim())) {
                panelP.setxCMomento(e.getX());

                panelP.repaint();
                panelP.validate();
            //}
        }
        else{
            if((e.getX() >= panelP.getxInicio()) && (e.getX() <= panelP.getxFim())){
                if((e.getY() >= panelP.getyInicio() - 5) && (e.getY() <= panelP.getyInicio() + 5)){
                    if (frame.getExibeMomento().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double momento = frame.getDiagramas().MomentoFletorNoPonto(x);
                        panelP.setMomentoFletor(true);
                        panelP.setxMomento(e.getX());
                        panelP.setyMomento(e.getY());
                        panelP.setValorMomento(momento);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeEsforco().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double esforco = frame.getDiagramas().EsforcoCortanteNoPonto(x);
                        panelP.setEsforcoCortante(true);
                        panelP.setxCortante(e.getX());
                        panelP.setyCortante(e.getY());
                        panelP.setValorCortante(esforco);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeDeformada().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double deformada = frame.getDiagramas().DeformadaNoPonto(x);
                        panelP.setDeformadaValor(true);
                        panelP.setxDeformada(e.getX());
                        panelP.setyDeformada(e.getY());
                        panelP.setValorDeformada(deformada);

                        panelP.repaint();
                        panelP.validate();
                    }
                }
            }
            else{
                 panelP.setMomentoFletor(false);
                 panelP.setEsforcoCortante(false);
                 panelP.setDeformadaValor(false);

                 panelP.repaint();
                 panelP.validate();
            }
        }
    }

    public void setPanelP(PanelPrincipal panelP) {
        this.panelP = panelP;
    }

    public double getForca() {
        return forca;
    }

    public void setFrame(JanelaPrincipal frame) {
        this.frame = frame;
    }

    private void exibeCaixaForca(){

    }




}

